using System;
using System.Collections.Generic;
using System.Text;
using SideShow.Contents;
using System.IO;

namespace SideShow.Channel.Messages
{
    [Serializable]
    class AddContentMessage : Message
    {
        private Guid _applicationId;
        private Guid _endpointId;
        private Content _content;

        public AddContentMessage()
        {
        }

        public AddContentMessage(Guid applicationId, Guid endpointId, Content content)
            : base(ChannelMessages.AddContent)
        {
            _applicationId = applicationId;
            _endpointId = endpointId;
            _content = content;
        }

        public Guid ApplicationId
        {
            get { return _applicationId; }
        }

        public Guid EndpointId
        {
            get { return _endpointId; }
        }

        public Content Content
        {
            get { return _content; }
        }

        protected internal override void SerializeObject(BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write(_applicationId.ToByteArray());
            writer.Write(_endpointId.ToByteArray());
            writer.Write(GetTypeName(_content));
            _content.SerializeObject(writer);
        }

        protected internal override void DeserializeObject(BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _applicationId = new Guid(reader.ReadBytes(16));
            _endpointId = new Guid(reader.ReadBytes(16));
            Type type = FindType(reader.ReadString());
            _content = (Content) Activator.CreateInstance(type);
            _content.DeserializeObject(reader);
        }
    }
}
